<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 播放音效12345678910111213141516171819202122#pragma mark 基本使用- (void)baseUse&amp;#123;    //1. 创建URL地址    NSURL *url = [[NSBundle mainBundle] URLForResource:@&amp;quot;buyao.wav&amp;quot; withExtension:nil];">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS-Audio(音频)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-Audio(音频)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 播放音效12345678910111213141516171819202122#pragma mark 基本使用- (void)baseUse&amp;#123;    //1. 创建URL地址    NSURL *url = [[NSBundle mainBundle] URLForResource:@&amp;quot;buyao.wav&amp;quot; withExtension:nil];">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:53:45.344Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS-Audio(音频)">
<meta name="twitter:description" content="####1. 播放音效12345678910111213141516171819202122#pragma mark 基本使用- (void)baseUse&amp;#123;    //1. 创建URL地址    NSURL *url = [[NSBundle mainBundle] URLForResource:@&amp;quot;buyao.wav&amp;quot; withExtension:nil];">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-Audio(音频)/"/>





  <title>iOS-Audio(音频) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/iOS-Special-Function/iOS-Audio(音频)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">iOS-Audio(音频)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 播放音效<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark 基本使用</span><br><span class="line">- (void)baseUse</span><br><span class="line">&#123;</span><br><span class="line">    //1. 创建URL地址</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;buyao.wav&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 系统音效文件 SystemSoundID = UInt32</span><br><span class="line">    SystemSoundID soundID;</span><br><span class="line">    </span><br><span class="line">    //3. 创建音效文件</span><br><span class="line">    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &amp;soundID);</span><br><span class="line">    </span><br><span class="line">    //4. 播放音效文件</span><br><span class="line">    //4.1 不带震动的播放</span><br><span class="line">    //AudioServicesPlaySystemSound(soundID);</span><br><span class="line">    </span><br><span class="line">    //4.2 带振动的播放 --&gt; 真机才有效果</span><br><span class="line">    AudioServicesPlayAlertSound(soundID);</span><br><span class="line">    </span><br><span class="line">    //5. 如果不需要播放了, 需要释放音效所占用的内存</span><br><span class="line">    AudioServicesDisposeSystemSoundID(soundID);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>####2. 简单封装<br>MTAudioTool.h<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#import &lt;Foundation/Foundation.h&gt;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line"></span><br><span class="line">@interface MTAudioTools : NSObject</span><br><span class="line">/** 播放系统音效*/</span><br><span class="line">+ (void)playSystemSoundWithURL:(NSURL *)url;</span><br><span class="line">/** 播放震动音效*/</span><br><span class="line">+ (void)playAlertSoundWithURL:(NSURL *)url;</span><br><span class="line">/** 清空音效文件的内存*/</span><br><span class="line">+ (void)clearMemory;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>MTAudioTool.m<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#import &quot;MTAudioTools.h&quot;</span><br><span class="line"></span><br><span class="line">/** 缓存字典*/</span><br><span class="line">static NSMutableDictionary *_soundIDDict;</span><br><span class="line"></span><br><span class="line">@implementation MTAudioTools</span><br><span class="line"></span><br><span class="line">// 只要头文件参与了编译调用</span><br><span class="line">//+ (void)load</span><br><span class="line"></span><br><span class="line">/** 缓存字典初始化*/</span><br><span class="line">+ (void)initialize</span><br><span class="line">&#123;</span><br><span class="line">    _soundIDDict = [NSMutableDictionary dictionary];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">+ (void)playSystemSoundWithURL:(NSURL *)url</span><br><span class="line">&#123;</span><br><span class="line">    // 不带震动的播放</span><br><span class="line">    AudioServicesPlaySystemSound([self loadSoundIDWithURL:url]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/** 播放震动音效*/</span><br><span class="line">+ (void)playAlertSoundWithURL:(NSURL *)url</span><br><span class="line">&#123;</span><br><span class="line">    // 带震动的播放</span><br><span class="line">    AudioServicesPlayAlertSound([self loadSoundIDWithURL:url]);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 播放音效的公用方法</span><br><span class="line">+ (SystemSoundID)loadSoundIDWithURL:(NSURL *)url</span><br><span class="line">&#123;</span><br><span class="line">    // 思路思路</span><br><span class="line">    // soundID重复创建 --&gt; soundID每次创建, 就会有对应的URL地址产生</span><br><span class="line">    // 可以将创建后的soundID 及 对应的URL 进行缓存处理</span><br><span class="line">    </span><br><span class="line">    //1. 获取URL的字符串</span><br><span class="line">    NSString *urlStr = url.absoluteString;</span><br><span class="line">    </span><br><span class="line">    //2. 从缓存字典中根据URL来取soundID 系统音效文件</span><br><span class="line">    SystemSoundID soundID = [_soundIDDict[urlStr] intValue];</span><br><span class="line">    </span><br><span class="line">    //需要在刚进入的时候, 判断缓存字典是否有url对应的soundID</span><br><span class="line">    </span><br><span class="line">    //3. 判断soundID是否为0, 如果为0, 说明没有找到, 需要创建</span><br><span class="line">    if (soundID == 0) &#123;</span><br><span class="line">        //3.1 创建音效文件</span><br><span class="line">        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &amp;soundID);</span><br><span class="line">        </span><br><span class="line">        //3.2 缓存字典的添加键值</span><br><span class="line">        _soundIDDict[urlStr] = @(soundID);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    return soundID;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">/** 清空音效文件的内存*/</span><br><span class="line">+ (void)clearMemory</span><br><span class="line">&#123;</span><br><span class="line">    //1. 遍历字典</span><br><span class="line">    [_soundIDDict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) &#123;</span><br><span class="line">        </span><br><span class="line">        //2. 清空音效文件的内存</span><br><span class="line">        SystemSoundID soundID = [obj intValue];</span><br><span class="line">        AudioServicesDisposeSystemSoundID(soundID);</span><br><span class="line">    &#125;];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>ViewController.m<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line">#import &quot;MTAudioTools.h&quot;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> 1. 导入AVFoundation框架</span><br><span class="line"> 2. 创建音效文件</span><br><span class="line"> 3. 播放音效文件</span><br><span class="line"> </span><br><span class="line"> 音效: 非常短的音乐, 一般来说30秒以内的声音, 都算作音效</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    // Do any additional setup after loading the view, typically from a nib.</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 点击播放音效</span><br><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event</span><br><span class="line">&#123;</span><br><span class="line">    //1. 获取URL地址</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;buyao.wav&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 调用工具类播放音效</span><br><span class="line">    //[MTAudioTools playSystemSoundWithURL:url];</span><br><span class="line">    </span><br><span class="line">    [MTAudioTools playAlertSoundWithURL:url];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 当前控制器收到内存警告时会调用的方法</span><br><span class="line">- (void)didReceiveMemoryWarning</span><br><span class="line">&#123;</span><br><span class="line">    // 局部音效需要在这里进行释放</span><br><span class="line">    [MTAudioTools clearMemory];</span><br><span class="line">    NSLog(@&quot;%s&quot;,__func__);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####3. 音乐播放<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> 1. 需要使用AVFoundatiaon框架</span><br><span class="line"> 2. 创建音乐播放器</span><br><span class="line"> 3. 根据需求, 进行播放/暂停/停止</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">@interface ViewController ()</span><br><span class="line">@property (nonatomic, strong) AVAudioPlayer *player;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    </span><br><span class="line">    // 创建音乐播放器</span><br><span class="line">    </span><br><span class="line">    //1. 获取URL路径</span><br><span class="line">    NSURL *url = [[NSBundle mainBundle] URLForResource:@&quot;我爱你你却爱着她.mp3&quot; withExtension:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 创建一个error对象</span><br><span class="line">    NSError *error;</span><br><span class="line">    </span><br><span class="line">    //3. 创建音乐播放器</span><br><span class="line">    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&amp;error];</span><br><span class="line">    </span><br><span class="line">    if (error) &#123;</span><br><span class="line">        NSLog(@&quot;有错误产生&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)playClick:(id)sender &#123;</span><br><span class="line">    //1. 准备播放 --&gt; 将音频文件加载到内存中 --&gt; 这句话可以不写 --&gt; play会隐式调用prepareToPlay方法. 但是规范来说, 还是会写上</span><br><span class="line">    [self.player prepareToPlay];</span><br><span class="line">    </span><br><span class="line">    //2. 开始播放</span><br><span class="line">    [self.player play];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)pauseClick:(id)sender &#123;</span><br><span class="line"></span><br><span class="line">    // 暂停播放</span><br><span class="line">    [self.player pause];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)stopClick:(id)sender &#123;</span><br><span class="line">    // 停止播放</span><br><span class="line">    [self.player stop];</span><br><span class="line">    </span><br><span class="line">    // 归零操作 / 时间重置 currentTime--&gt; 秒为单位</span><br><span class="line">    self.player.currentTime = 0;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####4. 录音<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AVFoundation/AVFoundation.h&gt;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> 1. 需要使用AVFoundatiaon框架</span><br><span class="line"> 2. 创建录音对象</span><br><span class="line"> 3. 根据需求, 进行录音/暂停/停止</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">@interface ViewController ()</span><br><span class="line"></span><br><span class="line">@property (nonatomic, strong) AVAudioRecorder *recorder;</span><br><span class="line"></span><br><span class="line">@property (nonatomic, strong) CADisplayLink *displayLink;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> </span><br><span class="line"> //录音</span><br><span class="line"> //    //settings  设置参数  录音相关参数  声道  速率  采样率</span><br><span class="line"> //    NSMutableDictionary *setting = [NSMutableDictionary dictionary];</span><br><span class="line"> //    // 音频格式</span><br><span class="line"> //    setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);</span><br><span class="line"> //    // 音频采样率</span><br><span class="line"> //    setting[AVSampleRateKey] = @(600.0);</span><br><span class="line"> //    // 音频通道数</span><br><span class="line"> //    setting[AVNumberOfChannelsKey] = @(1);</span><br><span class="line"> //    // 线性音频的位深度</span><br><span class="line"> //    setting[AVLinearPCMBitDepthKey] = @(8);</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    </span><br><span class="line">    // 创建录音对象</span><br><span class="line">    //1. 获取URL地址 --&gt; 具体的文件名路径</span><br><span class="line">    //--&gt; 注意之前是获取资源地址, 这里是指要将录音存放到哪里</span><br><span class="line">    NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@&quot;recorde.wav&quot;];</span><br><span class="line">    </span><br><span class="line">    //2. 将path字符串转换成NSURL --&gt; file://</span><br><span class="line">    NSURL *url = [NSURL fileURLWithPath:path];</span><br><span class="line">    </span><br><span class="line">    //3. 配置设置字典</span><br><span class="line">    // 如果传空, 默认就是高质量</span><br><span class="line">    // 录音的参数值</span><br><span class="line">    NSMutableDictionary *setting = [NSMutableDictionary dictionary];</span><br><span class="line">    </span><br><span class="line">    //4. 创建Error对象 __autoreleasing 可以不加, 加上之后是最标准写法</span><br><span class="line">    __autoreleasing NSError *error;</span><br><span class="line">    </span><br><span class="line">    //5. 创建录音对象</span><br><span class="line">    self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setting error:&amp;error];</span><br><span class="line">    </span><br><span class="line">//    if (error) &#123;</span><br><span class="line">//        NSLog(@&quot;error&quot;);</span><br><span class="line">//    &#125;</span><br><span class="line">    </span><br><span class="line">    //6. 打开分贝的检测</span><br><span class="line">    self.recorder.meteringEnabled = YES;</span><br><span class="line">    </span><br><span class="line">    //7. 如果要在真机运行, 还需要一个session类, 并且制定分类为录音</span><br><span class="line">    AVAudioSession *session = [AVAudioSession new];</span><br><span class="line">    [session setCategory:AVAudioSessionCategoryRecord error:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)recordClick:(id)sender &#123;</span><br><span class="line">    //1. 准备录音</span><br><span class="line">    [self.recorder prepareToRecord];</span><br><span class="line">    </span><br><span class="line">    //2. 开始录音  --&gt; 如果同一路径再次录音, 则会覆盖之前的文件</span><br><span class="line">    [self.recorder record];</span><br><span class="line">    </span><br><span class="line">    //3. 进行分贝的循环检测 --&gt; 添加计时器</span><br><span class="line">    [self updateMetering];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)pauseClick:(id)sender &#123;</span><br><span class="line">    // 暂停录音  --&gt; 如果用户只是暂停了, 应该提示用户进行保存操作</span><br><span class="line">    [self.recorder pause];</span><br><span class="line">    </span><br><span class="line">    // 暂停循环</span><br><span class="line">    self.displayLink.paused = YES;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (IBAction)stopClick:(id)sender &#123;</span><br><span class="line">    // 停止录音 --&gt; 之后停止录音时, 最终的录音文件才会生产</span><br><span class="line">    [self.recorder stop];</span><br><span class="line">    </span><br><span class="line">    // 暂停循环</span><br><span class="line">    self.displayLink.paused = YES;</span><br><span class="line">    </span><br><span class="line">    NSLog(@&quot;停止录音&quot;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 添加计时器</span><br><span class="line">- (void)updateMetering</span><br><span class="line">&#123;</span><br><span class="line">    // 如果没有displayLink就创建</span><br><span class="line">    if (self.displayLink == nil) &#123;</span><br><span class="line">        //1. 创建displayLink</span><br><span class="line">        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateMeter)];</span><br><span class="line">        </span><br><span class="line">        //2. 添加到运行循环中</span><br><span class="line">        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    // 判断如果暂停了循环, 就打开</span><br><span class="line">    if (self.displayLink.isPaused) &#123;</span><br><span class="line">        self.displayLink.paused = NO;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 循环调用的方法</span><br><span class="line">- (void)updateMeter</span><br><span class="line">&#123;</span><br><span class="line">    //需求: 自动停止录音 --&gt; 根据分贝的大小来判断</span><br><span class="line">    </span><br><span class="line">    //1. 我们需要获取分贝信息</span><br><span class="line">    //2. 设置分贝如果小于某个值, 一定时间后, 自动停止</span><br><span class="line">    </span><br><span class="line">    //1. 更新分贝信息</span><br><span class="line">    [self.recorder updateMeters];</span><br><span class="line">    </span><br><span class="line">    //2. 获取分贝信息 --&gt; iOS直接传0</span><br><span class="line">    // 0 ~ -160 , 值最大是0, 最小是-160. 系统返回的是负值</span><br><span class="line">    CGFloat power = [self.recorder averagePowerForChannel:0];</span><br><span class="line">    </span><br><span class="line">    //3. 实现2S自动停止</span><br><span class="line">    static NSInteger number;</span><br><span class="line">    </span><br><span class="line">    //displayLink,一秒默认是60次, 如果120此的调用都小于某个分贝值, 我们就可以认为要自动停止</span><br><span class="line">    </span><br><span class="line">    //3.1 先判断用户是否小于某个分贝值 --&gt; 用户是否没说话</span><br><span class="line">    if (power &lt; -30) &#123;</span><br><span class="line">        </span><br><span class="line">        //3.2 如果发现很安静, 我们就可以记录一下, number进行叠加</span><br><span class="line">        number++;</span><br><span class="line">        </span><br><span class="line">        //3.3 如果发现120次了, 都小于设定的分贝值</span><br><span class="line">        if (number / 60 &gt;= 2) &#123;</span><br><span class="line">            </span><br><span class="line">            //3.4 调用停止方法</span><br><span class="line">            [self stopClick:nil];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        number = 0;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    NSLog(@&quot;power: %f&quot;,power);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####</p>
<p>####</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Sort/iOS-NSString-加密/" rel="next" title="iOS-NSString-加密">
                <i class="fa fa-chevron-left"></i> iOS-NSString-加密
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Special-Function/iOS-Bluetooth(蓝牙)/" rel="prev" title="iOS-Bluetooth(蓝牙)">
                iOS-Bluetooth(蓝牙) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
